/**
 * Created by Amaris on 2016/12/6.
 */


var util = require('../utils/util.js');
var teamClass = require('../class/team.js');

module.exports = {

    create: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var team = new teamClass();

        var checkResult = team.checkParams(
            [
                'name',
                'createUid'
            ],
            params);
        if (checkResult) {
            cb(util.getErrorRes('400', checkResult), null);
            return;
        } else {
            team.init(params);
        }

        models.team.findOrCreate(team.getParams(), function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    },

    getInfo: function (params, cb) {
        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var team = new teamClass();

        var checkResult = team.checkParams(['rowId'], params);
        if (checkResult) {
            cb(util.getErrorRes('400', checkResult), null);
            return;
        } else {
            team.init(params);
        }

        models.team.findOne(team.getParams(), function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));

        });
    },
    getList: function (params, cb) {

        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = "数据库连接失败";
            cb(err, null);
            return;
        }

        models.team.find().exec(function (err, data) {
            if (err) {
                return res.serverError(err);
            }
            cb(null, util.getSuccessRes(data));
        });
    },
    getListByRowIds: function (params, cb) {

        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = "数据库连接失败";
            cb(err, null);
            return;
        }

        var expr = {rowId: params};

        models.team.find(expr).exec(function (err, data) {
            if (err) {
                cb(err, null);
            }
            cb(null, util.getSuccessRes(data));
        });
    }

};



